<?php
	function f200($sql){
		//create time handle instance
		$dataHandle = new DataHandle();
		$dataHandle->setRequest("200");

		//array_push($format,{"#Date#","date","Y.m.d"});
		//array_push($format,{"#Item#","number","6"});
		//process time filter control parametars
		//$dataHandle->processTime($startTime, $endTime);
		//apply additional filters from time filter control
		//$filter = $dataHandle->processFilters($startTime, $endTime, "lin_produced");
		//generate query string

		$dataType = getValue($_GET['dataType']);
		$action = getValue($_GET['action']);

		if($action == "getItems"){
			$format = array(
				"start_time" => "dateY.m.d",
				"product_id" => "numb6",
			);
			$dataHandle->setFormat($format);
			$error = $dataHandle->processTime($startTime, $endTime);
			if($error == ""){
				$filter = $dataHandle->processFilters($startTime, $endTime, "lin_produced","start_time");
				$query = getJSONQuery($startTime, $endTime, $filter);
				print($dataHandle->printJSON($sql, $query));
			}
			else
				print($error);
		}
		else if($dataType == "csv"){
			$format = array(
				"Date" => "dateY.m.d",
				"StartTime" => "dateY.m.d",
				"EndTime" => "dateY.m.d",
			);
			$dataHandle->setFormat($format);
			$error = $dataHandle->processTime($startTime, $endTime);
			if($error == ""){
				$filter = $dataHandle->processFilters($startTime, $endTime, "lin_produced_export_view", "start_time");
				$query = getCSVQuery($startTime, $endTime, $filter);
				$dataHandle->printCSV($sql, $query);
			}
			else
				print($error);
		}
		else if($action == "getYears"){
			$query = getYears();
			print($dataHandle->printJSON($sql, $query));
		}
		else if($action == "getLines"){
			$query = getLines();
			print($dataHandle->printJSON($sql, $query));
		}
		else{
			$temp = array();
			$temp['error'] = "200: Bad request";
			print(json_encode($temp));
		}

		unset($dataHandle);
	}
	function getJSONQuery(&$startTime, &$endTime, &$filter){

		$queryString = 	
		"
			SELECT
			lin_produced.id_pk as 'ID',
			lin_produced.start_time,
			lin_produced.product_id,
			lin_products.description,
			lin_produced.order_id,
			lin_produced.line_id, 
			lin_produced.minutes
			
			FROM lin_produced
			INNER JOIN lin_products
			ON lin_produced.product_id=lin_products.product_id";


		$queryString = $queryString.$filter;

		$oderByFiled = getValue($_GET['sortItem']);
		$oderByValue = getValue($_GET['ascDesc']);

		if($oderByFiled !== "" and $oderByValue !== "")
			$queryString = $queryString." order by ".$oderByFiled." ".$oderByValue;

		return $queryString;
	}

	function getCSVQuery(&$startTime, &$endTime, &$filter){
		$queryString = 	
			"
			SELECT
			date as 'Date',
			shift as Shift,
			line_id as Line,
			speed as Speed,
			product_id  as Item#,
			start_time as StartTime, 
			end_time as EndTime, 
			minutes as Minutes,
			description as Description,
			pouches_per_unit as PouchesPerCase,
			pouches as PouchesActual,
			pouches_total as PouchesTotal,
			packers as Packers,
			attendant as Attendant,
			production_efficiency as PE,
			technical_efficiency as TE
			FROM lin_produced_export_view";

		$queryString = $queryString.$filter;

		$queryString = $queryString."order by end_time desc;";

		return $queryString;
	}
	function getYears(){
		$queryString = 
			"SELECT DISTINCT
			year(start_time) AS availableYear
			FROM lin_produced
			Order by availableYear DESC";
		return $queryString;
	}
	function getLines(){
		$queryString = 
			"SELECT Distinct
			line_id as availableLine
			FROM lin_produced
			ORDER BY availableLine ASC";
		return $queryString;
	}
?>
